package com.xzqwjw.taskmanager.activity;

import java.util.List;
import java.util.Map;

import com.xzqwjw.taskmanager.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.widget.ListView;

public class ActivityFavorite extends Activity {

  private String strDBFile;
  private List<Map<String, Object>> listData = null;
  private ListView lvFavorite = null;
  private ProgressDialog objPD;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_favorite);

    Intent objIT = getIntent();
    strDBFile = objIT.getStringExtra("DBFilePath");

    lvFavorite = (ListView) findViewById(R.id.lvFavorite);
    new AsyncLoadFavoriteList().execute();
  }

  class AsyncLoadFavoriteList extends android.os.AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
      objPD = ProgressDialog.show(ActivityFavorite.this, "读取数据",
          "正在读取收藏的人员数据，请稍等！", true, false);
    }

    @Override
    protected Void doInBackground(Void... params) {
      listData = com.leaderinfo.service.ItemDataService.getInfosByFavorite(strDBFile);
      return null;
    }

    @Override
    protected void onPostExecute(Void result) {
      super.onPostExecute(result);
      if (listData != null) {
        com.leaderinfo.utility.ListItemFavoriteAdapter objAdapter = new com.leaderinfo.utility.ListItemFavoriteAdapter(
            ActivityFavorite.this, listData, strDBFile);
        lvFavorite.setAdapter(objAdapter);
      } else {
        AlertDialog.Builder objDialog = new AlertDialog.Builder(
            ActivityFavorite.this);
        objDialog.setIcon(R.drawable.ic_launcher);
        objDialog.setTitle("提示！");
        objDialog.setMessage("您还没有收藏任何资料！");
        objDialog.setPositiveButton("确定",
            new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog,
                                  int which) {
                Intent objIT = new Intent(
                    ActivityFavorite.this,
                    ActivityMain.class);
                objIT.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK
                    | Intent.FLAG_ACTIVITY_NEW_TASK);
                objIT.putExtra("DBFilePath", strDBFile);
                startActivity(objIT);
              }
            });
        objDialog.show();
      }
      objPD.dismiss();
    }

  }

}
